package com.quanyan.club.web.admin;

import com.google.common.base.Strings;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.entity.admin.ClubActivityOrderDetail;
import com.quanyan.club.entity.vo.req.admin.ReqActivityOrderQuery;
import com.quanyan.club.entity.vo.resp.admin.ResActivityOrderVo;
import com.quanyan.club.service.ClubActivityAdminService;
import com.quanyan.club.service.ClubActivityService;
import com.quanyan.club.web.BaseController;
import com.quanyan.common.Plugin.PageObj;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author Hugo.Wwg
 * @Since 2016-08-22
 */
@RestController
@RequestMapping("/admin/api/club/activity")
public class ActivityOrderAdminController extends BaseController {

    @Autowired
    private ClubActivityAdminService clubActivityAdminService;
    @Autowired
    private ClubActivityService clubActivityService;

    @RequestMapping(value = "/queryActivityOrderList", method = RequestMethod.POST)
    public APIResponse<PageObj<List<ResActivityOrderVo>>> queryActivityOrderList(
            @RequestBody ReqActivityOrderQuery reqActivityOrderQuery) {
        return clubActivityAdminService.queryActivityOrderList(reqActivityOrderQuery);
    }

    @RequestMapping(value = "/activityOrderDetail", method = RequestMethod.POST)
    public APIResponse<ClubActivityOrderDetail> activityOrderDetail(
            @RequestBody Map<String, Object> map) {
        Integer orderId = MapUtils.getInteger(map, "orderId");
        Integer activityId = MapUtils.getInteger(map, "activityId");
        return clubActivityAdminService.activityOrderDetail(orderId, activityId);
    }

    /**
     * 管理员取消未支付订单
     */
    @RequestMapping(value = "/cancelNotPayActivityOrder", method = RequestMethod.POST)
    public APIResponse cancelNotPayActivityOrder(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request) {
        Integer adminId=MapUtils.getInteger(map, "adminId");
        if (adminId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        Integer uid = MapUtils.getInteger(map, "uid");
        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        Integer activityId = MapUtils.getInteger(map, "activityId");
        String businessOrderNo = MapUtils.getString(map, "businessOrderNo");

        if (activityId == null || activityId <= 0 || Strings.isNullOrEmpty(businessOrderNo)) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }

        return clubActivityAdminService.cancelNotPayActivityOrder(adminId,uid, activityId, businessOrderNo);
    }
    /**
     * 订单退款
     */
    @RequestMapping(value = "/cancelActivityOrder", method = RequestMethod.POST)
    public APIResponse cancelActivityOrder(@RequestBody Map<String, String> map, HttpServletRequest request) {
        Integer adminId=MapUtils.getInteger(map, "adminId");
        if (adminId == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        if (!map.containsKey("businessOrderNo")) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        Integer uid = MapUtils.getInteger(map, "uid");
        if (uid == null) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return clubActivityService.cancelOrderNotify(uid, (String) map.get("businessOrderNo"), 1, "管理员取消");
    }
    //public APIResponse<?>
}
